﻿using System;

namespace CSharp进阶_事件
{
	class EventClass
	{
		public event Action myEvent;
		public void Test()
		{
			myEvent = null;
			myEvent += Func1;
			myEvent += Func2;
			myEvent.Invoke();
			myEvent();
			myEvent -= Func1;
			myEvent.Invoke();
			myEvent();
		}
		public void Func1()
		{
			Console.WriteLine("func1");
		}
		public void Func2()
		{
			Console.WriteLine("func2");
		}
	}
	class Heater
	{
		public event Action<int> heatAlarm;
		public void PlusHeat(ref int temp)
		{
			if (temp >= 95)
			{
				if(heatAlarm != null)
				{
					heatAlarm(temp);
					heatAlarm = null;
				}
			}
			else
			{
				temp++;
				Console.WriteLine("加热: {0}`c", temp);
			}
		}
	}
	class Alarm
	{
		public void Alarms(int temp)
		{
			Console.WriteLine("温度 {0} 过热，请关机", temp);
		}
	}
	class Display
	{
		public void Show(int temp)
		{
			Console.WriteLine("显示温度：" + temp);
		}
	}

	class WaterHeater
	{
		public int temp = 20;
		public Heater heater = new Heater();
		public Alarm alarm = new Alarm();
		public Display display = new Display();
		public WaterHeater()
		{
			heater.heatAlarm += alarm.Alarms;
			heater.heatAlarm += display.Show;
		}
		public void Run()
		{
			int updateIndex = 0;
			while (true)
			{
				if (updateIndex % 99999999 == 0)
				{
					if (temp >= 100)
					{
						break;
					}
					heater.PlusHeat(ref temp);
					updateIndex = 0;
				}
				updateIndex++;
			}
		}
	}
	class Program
	{
		static void Main(string[] args)
		{
			WaterHeater w = new WaterHeater();
			w.Run();

			//EventClass ec = new EventClass();
			//ec.Test();
		}
	}
}
